
const request =require('./request')
const Router = {
    async index( query ) {
        const { cid } = query;
        if (!cid) {
            return {
                result: 500,
                errMsg: 'cid ?',
            };
        }
        let obj = {};
        const sInfo = await request( `https://c.musicapp.migu.cn/MIGUM2.0/v1.0/content/resourceinfo.do?copyrightId=${cid}&resourceType=2`);
        if (sInfo.resource && sInfo.resource[0]) {
            //console.log(sInfo)
            const data = sInfo.resource[0];
            obj.picUrl = (data.albumImgs || [{ img: '' }])[0].img;
            obj.bigPicUrl = obj.picUrl;
            obj.cid = cid;
            obj.id = data.songId;
            obj.name = data.songName;
            const typeMap = {
                PQ: '128',
                HQ: '320',
                SQ: 'flac',
            };
            (data.newRateFormats || data.rateFormats || []).forEach(({ formatType, androidUrl, url = androidUrl }) => {
                if (typeMap[formatType]) {
                    obj[typeMap[formatType]] = url.replace(/ftp:\/\/[^/]+/, 'https://freetyst.nf.migu.cn');
                }
            });
            obj.artists = data.artists;

        }

        return {
            result: 100,
            data: obj,
        };
    }

};
exports.default = Router;
